let cnst = require('./../const');

const mysqlPool = require('./MySQLPool');
class FriendDao {
    constructor() {
    }

    async searchFriend(uid, phone) {
        let sql = "select * from users where uid=? or phone=? ";
        if(!phone){
            phone = "-1";
        }

        let args = [uid, phone];
        let res = await mysqlPool.query(sql, args);
        if (!res) {
            let rs = res[0];
            let ret = { name: rs.name, headimgurl: rs.headimgurl, uid: rs.uid };
            return ret;
        }
      
        return null;
    }

    async deleteFriend(uid, friendUid) {
        let n1 = friendUid, n2 = uid;
        let unionId = n1 + '_' + n2;
        if (n1 > n2) unionId = n2 + '_' + n1;
        let sql = "delete from friend where unionId=?";
        let args = [unionId];
        let res = await mysqlPool.query(sql, args);
        if (res) {
            return res.affectedRows;
        }

        return 0;
    }
    async isFriend(key) {
        let sql = "select * from friend where unionId=?";
        let args = [key];
        let res = await mysqlPool.query(sql, args);
        if (res && res.length > 0) {
            return res[0].bFriend;
        }

        return null;
    }

    //申请成为好友
    async applyBeFriend(owner, guest) {

        let n1 = guest.uid, n2 = owner.uid;
        let unionId = n1 + '_' + n2;
        if (n1 > n2) {
            unionId = n2 + '_' + n1;
        }
        let sql = "select * from friend where unionId=?";
        let args = [unionId];
        
        let res = await mysqlPool.query(sql, args);
        //先判断申请是否一致
        //如果是 11 表示双方已经是好友，无需申请
        //如果是 0或nil 表示一方申请过好友，但对方拒绝了，重新置01或10(对方拒绝，记录会被删除,这条可以忽略)
        //如果01或10 若数字相反，说明对方已经申请您为好友，直接双方成为好友。 若数字一致，说明之前有申请过，不做处理
        if (res) {
            // let info = res[0];
            // if (info.agree === 1 && n1 > n2){
            //     return await this.agreeApllyFriend(n2,n1);
            // }else if(info.agree === 10 && n1 < n2){
            //     return await this.agreeApllyFriend(n2,n1);
            // }
            return res[0];
        }

        return null;
    }

    async updateAgree(id, agree){
        let sql = "update friend set agree = ? where id = ?";
        let args = [agree, id];
        let res = await mysqlPool.query(sql, args);
        if (res) {
            return res.affectedRows;
        }

        return 0;
    }

    async save(owner, guest){
        let n1 = Number(guest.uid);
        let n2 = Number(owner.uid);
        let unionId = n1 + '_' + n2;
        if (n1 > n2) unionId = n2 + '_' + n1;

        let agree = 1;
        if (n2 < n1) {
            agree = 10;
            let temp = owner;
            owner = guest;
            guest = temp;
        }

        let sql = 'insert into friend (unionId,myuid,frienduid,time,  tick,    myname,friendName,friend_imgurl,my_imgurl,agree)' +
        '                       values(?,      ?,    ?,        now(), ?,       ?,     ?,         ?,            ?,        ?)';
        let args = [unionId, owner.uid, guest.uid, new Date().getTime(), owner.name, guest.name, guest.headimgurl, owner.headimgurl, agree];
        console.logw('插入房主申请成为好友记录', args);
        let res = await mysqlPool.query(sql, args);
        if(res){
            return res.insertId;
        }
        //100117_100118    100117同意 就是10    100118同意就是  01  两个都同意就是  11  为0就是拒绝

        return 0;
    }



    async agreeApllyFriend(uid, friendUid) {
        let n1 = Number(uid), n2 = Number(friendUid);
        let unionId = n1 + '_' + n2;
        if (n1 > n2) unionId = n2 + '_' + n1;
        let sql = "select * from friend where unionId=?";
        let args = [unionId];
        let res = await mysqlPool.query(sql, args);
        if (res) {
            let info = res[0];
            if (n1 > n2) {
                info.agree = Math.floor(info.agree / 10) * 10 + 1;
            }

            if (n1 < n2) {
                info.agree = 10 + info.agree % 10;
            }

            if (info.agree === 11) { 
                sql = 'update friend set bFriend=1, agree=? where unionId=?'; 
            } else {
                sql = 'update friend set bFriend=0, agree=? where unionId=? '; 
            }

            let args = [info.agree, unionId];
            await mysqlPool.query(sql, args);
            return info;
        }

        return null;
    }

    async getFriendsByPage(uid, page) {
        let offset = (page - 1) * 10;
        if (offset < 0) offset = 0;
        let sql = "select * from friend where myuid=? or frienduid=? order by Id desc limit " + offset + ",10";
        let args = [uid, uid];
        let res = await mysqlPool.query(sql, args);
        return res;
    }

}


let friendDao = new FriendDao();
module.exports = friendDao;
